/**
 * @desc
 *  
 * @fun setStorage & getStorage & removeStorage & clearStorage & getStorageInfo
 **/

const storage = {

  getStorage(key) {
    return new Promise((resolve, reject) => {
      // let options ={
      //   key:key,
      //   success:resolve,
      //   fail:reject
      // }
      wx.getStorage({
        key: key,
        success: function(res) {resolve(res)},
        fail:function(err){reject(err)}
      })
      // resolve();
      // wx.getStorage(options);
      // try{
      //  let storage= wx.getStorageSync(key);
      //  if(storage){
      //    resolve(storage);
      //  }
      // }catch(e){
      //   reject();
      // }
    })
  },

  setStorage(data) {
    return new Promise((resolve, reject) => {
      // wx.setStorage({
      //   key: data.key,
      //   data: data.data,
      //   success: resolve,
      //   fail: reject
      // })

      wx.setStorage({
        key: data.key,
        data: data.data,
        success: function(res) {
          resolve();
        },
        fail: function(res) {
          reject();
        },
        complete: function(res) {},
      })
    })
  },

  removeStorage(key) {
    return new Promise((resolve, reject) => {
      wx.removeStorage({
        key: key,
        success:()=>{
          resolve();
        },
        fail:()=>{
          reject();
        }
      })
    })
  },

  clearStorage() {
    return new Promise((resolve, reject) => {
      try {
        wx.clearStorageSync();
        resolve();
      } catch (e) {
        // Do something when catch error
        reject();
      }
    })
  },

  getStorageInfo(key) {
    return new Promise((resolve, reject) => {
      // wx.getStorageInfo({
      //   success: resolve,
      //   fail: reject
      // })
      wx.getStorageInfo({
        success: function(res) {
          resolve(res)
        },
        fail:function(err){
          reject(err)
        }
      })
    })
  }

}

module.exports = storage;
